package com.magic.gre.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.magic.gre.base.activity.BaseMVPActivity;
import com.magic.gre.helper.WXConfig;
import com.magic.gre.mvp.contract.WXEntryContract;
import com.magic.gre.mvp.presenter.WXEntryPresenterImpl;
import com.magic.gre.ui.activity.BindPhoneActivity;
import com.magic.gre.ui.activity.MainActivity;
import com.noname.lib_base_java.entity.MsgEvent;
import com.noname.lib_base_java.net.RxBus;
import com.noname.lib_base_java.util.L;
import com.noname.lib_base_java.util.ToastUtil;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.HashMap;

/**
 * Author: Lmoumou
 * Date: 2019-01-31 16:03
 * 文件名: WXEntryActivity
 * 描述:
 */
public class WXEntryActivity extends BaseMVPActivity<WXEntryPresenterImpl> implements IWXAPIEventHandler,
        WXEntryContract.View {

    private static final String TAG = "WXEntryActivity";

    private IWXAPI api;

    @Override
    protected int getLayoutId() {
        return 0;
    }

    @Override
    protected WXEntryPresenterImpl creatPresenter() {
        return new WXEntryPresenterImpl(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, WXConfig.APP_ID_WX, true);
        api.registerApp(WXConfig.APP_ID_WX);
        try {
            boolean b = api.handleIntent(getIntent(), this);
            if (!b) {
                finish();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    protected void netOverride() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        api.handleIntent(data, this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
        finish();
    }

    @Override
    public void onReq(BaseReq baseReq) {
        L.e(TAG, "onReq");
    }

    @Override
    public void onResp(BaseResp baseResp) {
        L.e(TAG, "onResp");
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                L.e(TAG, "发送成功");
                SendAuth.Resp resp = (SendAuth.Resp) baseResp;
                HashMap<String, Object> parmsMap = new HashMap<>();
                parmsMap.put("appid", WXConfig.APP_ID_WX);
                parmsMap.put("secret", WXConfig.APP_SECRET_WX);
                parmsMap.put("code", resp.code);
                parmsMap.put("grant_type", WXConfig.GRAN_TYPE);
                mPresenter.pAccessToken("https://api.weixin.qq.com/sns/oauth2/access_token", parmsMap);
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                L.e(TAG, "发送取消");
                finish();
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                L.e(TAG, "发送被拒绝");
                finish();
                break;
            case BaseResp.ErrCode.ERR_BAN:
                L.e(TAG, "签名错误");
                finish();
                break;
            default:
                L.e(TAG, "未知错误");
                finish();
                break;
        }
    }

    @Override
    public void vAccessToken(String accessToken, String openid) {
        mPresenter.pWxLogin(openid);
    }

    @Override
    public void vWxLoginSuc() {
        MainActivity.startThis(this);
        RxBus.getInstance().post(new MsgEvent(MsgEvent.LOGIN_REGISTER));
        finish();
    }

    @Override
    public void vWxLoginFail(int code, String openid, String msg) {
        if (code == 1012) {
            BindPhoneActivity.startThis(this, openid);
            finish();
        } else {
            ToastUtil.getInstance().showNormal(this, msg);
            finish();
        }
    }
}
